#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>


int fillData()
{
    sqlite3 * myDB = NULL;
    
    /* 关闭野指针 */
    const char * filename = "./test.db";
    int ret = sqlite3_open(filename, &myDB);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_open error:%s\n", sqlite3_errmsg(myDB));
        exit(-1);
    }

    /* 1. 新建数据库表 userInfo */
    const char * sql = "create table IF NOT EXISTS userInfo (                 \
                            userId int primary key not null ,                  \
                            uesrname text not null,             \
                            passwd text not null                \
                            );";
    ret = sqlite3_exec(myDB, sql, NULL, NULL, NULL);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_exec1 error:%s\n", sqlite3_errmsg(myDB));
        exit(-1);
    }

    /* 需求: 手动输入ID & 用户名 & 密码 放到数据库里面去 */
    int id = 666;
    char *name = "tuantuan";
    char *passwd = "257257";

    /* 2. 插入数据 */
    char m_sql[128] = {0};
    sprintf(m_sql, "insert into userInfo values (%d, '%s', '%s');", id, name, passwd);
    ret = sqlite3_exec(myDB, m_sql, NULL, NULL, NULL);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_exec2 error:%s\n", sqlite3_errmsg(myDB));
        exit(-1);
    }

    /* 关闭数据库 */
    sqlite3_close(myDB);
}


int demoSelect()
{
    sqlite3 * myDB = NULL;
    
    /* 关闭野指针 */
    const char * filename = "./test.db";
    int ret = sqlite3_open(filename, &myDB);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_open error:%s\n", sqlite3_errmsg(myDB));
        exit(-1);
    }

    const char * sql = "select * from userInfo;";
    int row = 0;
    int column = 0;
    char **result = NULL;
    
    ret = sqlite3_get_table(myDB, sql, &result, &row, &column, NULL);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_get_table error");
        exit(-1);
    }
    printf("row:%d,\t column:%d\n", row, column);

    for (int idx = 1; idx <= row; idx++)
    {
        for (int jdx = 0; jdx < column; jdx++)
        {
            printf("result[%d] = %-15s\n", idx * column + jdx, result[idx * column + jdx]);
        }
        printf("\n");
    }
    /* 关闭数据库 */
    sqlite3_close(myDB);
}


int demoStringToInt(const char * str)
{
    #if 0
    int ret = atoi(str);
    printf("ret:%d\n", ret);
    #else

    char *endPtr = NULL;
    int ret = strtol(str, &endPtr, 10);
    printf("ret:%d\n", ret);
    printf("endptr:%s\n", endPtr);

    #endif
}
int main()
{
    #if 1
    sqlite3 * myDB = NULL;
    
    /* 关闭野指针 */
    const char * filename = "./test.db";
    int ret = sqlite3_open(filename, &myDB);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_open error:%s\n", sqlite3_errmsg(myDB));
        exit(-1);
    }

    // char name[32] = { 0 };
    // printf("input your name:");
    // scanf("%s", name);

    // char passwd[32] = { 0 };
    // printf("input your passwd:");
    // scanf("%s", passwd);

    #if 0
    /* 注册功能 - 需求: 看用户是否存在 */
    const char * sql = "select count(*) from userInfo where uesrname = 'zhangsan';";
    int row = 0;
    int column = 0;
    char **result = NULL;
    
    ret = sqlite3_get_table(myDB, sql, &result, &row, &column, NULL);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_get_table error");
        exit(-1);
    }
    printf("row:%d,\t column:%d\n", row, column);
    printf("result[0]:%s\n", result[1]);
    #else
    /* 登陆功能 - 需求: 看密码是否匹配 */
    const char * sql = "select passwd from userInfo where uesrname = 'tuantuan';";
    int row = 0;
    int column = 0;
    char **result = NULL;
    
    ret = sqlite3_get_table(myDB, sql, &result, &row, &column, NULL);
    if (ret != SQLITE_OK)
    {
        printf("sqlite3_get_table error\n");
        exit(-1);
    }
    printf("row:%d,\t column:%d\n", row, column);

    printf("result[0]:%s\n", result[0]);
    printf("result[1]:%s\n", result[1]);


    #endif
    // printf("登陆成功 ");
    // printf("登陆失败 ");
    #endif

}